/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sp.panelcomponent;

import java.awt.event.ItemEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import sp.dao.KecamatanDao;
import sp.dao.KelurahanDao;
import sp.dao.OptionB1r1Dao;
import sp.model.Dsartb1;
import sp.model.Kecamatan;
import sp.model.Kelurahan;
import sp.model.OptionB1r1;
import sun.java2d.loops.CompositeType;

/**
 *
 * @author Tiara Ratna Dewi [12.7403]
 */
public class DataEntryQuest extends javax.swing.JPanel {

    /**
     * Creates new form DataEntryQuest
     */
    private OptionB1r1 kodeKab;
    private Kecamatan kodeKec;

    public DataEntryQuest() {
        initComponents();
        sp.model.TableEntryModel tabModel = new sp.model.TableEntryModel();
        tableUpdate1.getTabelUpdate().setModel(tabModel);
        tableUpdate1.setLebarKolom2();
    }

    public void loadProgress() {
        String sql = "SELECT d FROM Dsartb1 d WHERE d.nim = \'" + sp.util.UserControl.getNim() + "\'";
        List<Dsartb1> list = sp.dao.DsartB1Dao.getInstance().entity().createQuery(sql).getResultList();
        int countDSART = list.size();
        if (countDSART > 0) {
            String sql1 = "SELECT b FROM B1 b WHERE b.nim = \'" + sp.util.UserControl.getNim() + "\'";
            List<sp.model.B1> list1 = sp.dao.B1Dao.getInstance().entity().createQuery(sql1).getResultList();
            int countQuest = list1.size();
            jProgressBar1.setMaximum(countDSART * 20);
            jProgressBar1.setValue(countQuest);
            jProgressBar1.setStringPainted(true);
            jLabel2.setText(countQuest + "/" + countDSART * 20);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        questDataEntry1 = new sp.component.QuestDataEntry();
        tableUpdate1 = new sp.panelcomponent.TableUpdate();
        startButton1 = new sp.componentButton.StartButton();
        backtobutton1 = new sp.componentButton.Backtobutton();
        comboKab = new javax.swing.JComboBox();
        comboKec = new javax.swing.JComboBox();
        comboKel = new javax.swing.JComboBox();
        searchButton1 = new sp.componentButton.SearchButton();
        jProgressBar1 = new javax.swing.JProgressBar();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        startButton1.setText("");
        startButton1.setEnabled(false);

        backtobutton1.setText("");

        comboKab.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Indramayu", "Bandung" }));
        comboKab.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                comboKabItemStateChanged(evt);
            }
        });

        comboKec.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        comboKec.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                comboKecItemStateChanged(evt);
            }
        });

        comboKel.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        searchButton1.setText("");
        searchButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButton1ActionPerformed(evt);
            }
        });

        jProgressBar1.setMaximum(120);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 102, 153));
        jLabel1.setText("Jumlah Kuesioner yang sudah di entry: ");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 102, 153));

        javax.swing.GroupLayout questDataEntry1Layout = new javax.swing.GroupLayout(questDataEntry1);
        questDataEntry1.setLayout(questDataEntry1Layout);
        questDataEntry1Layout.setHorizontalGroup(
            questDataEntry1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(questDataEntry1Layout.createSequentialGroup()
                .addGap(76, 76, 76)
                .addGroup(questDataEntry1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(questDataEntry1Layout.createSequentialGroup()
                        .addComponent(tableUpdate1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(54, 54, 54))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, questDataEntry1Layout.createSequentialGroup()
                        .addComponent(comboKab, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(comboKec, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(comboKel, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(searchButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(130, 130, 130)))
                .addGroup(questDataEntry1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(backtobutton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(startButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(questDataEntry1Layout.createSequentialGroup()
                        .addGap(122, 122, 122)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(questDataEntry1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 293, Short.MAX_VALUE)
                        .addComponent(jProgressBar1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(23, Short.MAX_VALUE))
        );
        questDataEntry1Layout.setVerticalGroup(
            questDataEntry1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(questDataEntry1Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(49, 49, 49)
                .addComponent(startButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(39, 39, 39)
                .addComponent(backtobutton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(230, 230, 230))
            .addGroup(questDataEntry1Layout.createSequentialGroup()
                .addGap(144, 144, 144)
                .addGroup(questDataEntry1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboKab, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboKec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboKel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tableUpdate1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(102, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(questDataEntry1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 20, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(questDataEntry1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 22, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void comboKabItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_comboKabItemStateChanged
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            kodeKab = (OptionB1r1) OptionB1r1Dao.getInstance().getSingleBy("OptionB1r1.findByKeterangan", "keterangan", comboKab.getSelectedItem().toString());
            comboKec.removeAllItems();
            fillComboKec(kodeKab.getKode());

            kodeKec = (Kecamatan) KecamatanDao.getInstance().getSingleBy("Kecamatan.findByNamakecamatan", "namakecamatan", comboKec.getSelectedItem().toString());
            comboKel.removeAllItems();
            fillComboKel(kodeKec.getIdkecamatan(), kodeKab.getKode());
        }
    }//GEN-LAST:event_comboKabItemStateChanged

    private void comboKecItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_comboKecItemStateChanged
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            kodeKec = (Kecamatan) KecamatanDao.getInstance().getSingleBy("Kecamatan.findByNamakecamatan", "namakecamatan", comboKec.getSelectedItem().toString());
            comboKel.removeAllItems();
            fillComboKel(kodeKec.getIdkecamatan(), kodeKab.getKode());
        }
    }//GEN-LAST:event_comboKecItemStateChanged

    private void searchButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButton1ActionPerformed
        searchPerformed();
    }//GEN-LAST:event_searchButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private sp.componentButton.Backtobutton backtobutton1;
    private javax.swing.JComboBox comboKab;
    private javax.swing.JComboBox comboKec;
    private javax.swing.JComboBox comboKel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JProgressBar jProgressBar1;
    private sp.component.QuestDataEntry questDataEntry1;
    private sp.componentButton.SearchButton searchButton1;
    private sp.componentButton.StartButton startButton1;
    private sp.panelcomponent.TableUpdate tableUpdate1;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the backtobutton1
     */
    public sp.componentButton.Backtobutton getBacktobutton1() {
        return backtobutton1;
    }

    /**
     * @return the startButton2
     */
    public sp.componentButton.StartButton getStartButton1() {
        return startButton1;
    }

    public TableUpdate getTableUpdate1() {
        return tableUpdate1;
    }

    public void fillCombo() {
        fillComboKab();
        comboKec.removeAllItems();
        fillComboKec(kodeKab.getKode());
        comboKel.removeAllItems();
        fillComboKel(kodeKec.getIdkecamatan(), kodeKab.getKode());

    }

    private void fillComboKab() {
        List<Object> kab = OptionB1r1Dao.getInstance().getBy("OptionB1r1.findByKeterangan", "keterangan", comboKab.getSelectedItem().toString());
        kodeKab = ((OptionB1r1) kab.get(0));
    }

    private void fillComboKec(String kodeKab) {
        List<Object> kec = KecamatanDao.getInstance().getBy("Kecamatan.findByKota", "kota", kodeKab);
        kec.stream().forEach((kec1) -> {
            comboKec.addItem(((Kecamatan) kec1).getNamakecamatan());
        });
    }

    private void fillComboKel(String kodeKec, String kodeKab) {
        String sql = "SELECT k FROM Kelurahan k WHERE k.idkecamatan =\'" + kodeKec + "\' AND  k.kota = \'" + kodeKab + "\'";
        List<Object> kel = (List<Object>) KelurahanDao.getInstance().entity().createQuery(sql).getResultList();
//        List<Object> kel = KelurahanDao.getInstance().getBy("Kelurahan.findByIdkecamatan", "idkecamatan", kodeKec);
        kel.stream().forEach((kel1) -> {
            comboKel.addItem(((Kelurahan) kel1).getNamakelurahan());
        });
    }

    private void searchPerformed() {
        OptionB1r1 kab1 = (OptionB1r1) OptionB1r1Dao.getInstance().getSingleBy("OptionB1r1.findByKeterangan", "keterangan", comboKab.getSelectedItem().toString());
        String Kab = kab1.getKode();
        Kecamatan kec4 = KecamatanDao.getInstance().getSingleBy("Kecamatan.findByNamakecamatan", "namakecamatan", comboKec.getSelectedItem().toString());
        String kec = kec4.getIdkecamatan();
        String sql = "SELECT k FROM Kelurahan k WHERE k.namakelurahan =\'" + comboKel.getSelectedItem().toString() + "\' AND k.idkecamatan =\'" + kec + "\' AND  k.kota = \'" + Kab + "\'";
        Kelurahan kel1 = (Kelurahan) KelurahanDao.getInstance().entity().createQuery(sql).getSingleResult();
        String kel = kel1.getIdkelurahan();
        String code_key = Kab + kec + kel;

        String refreshTxt = "";
        try {
//            List<Object> results = sp.dao.DsartB4Dao.getInstance().getAll();
            String sql1 = "SELECT d FROM Dsartb4 d WHERE d.nuart LIKE \'" + code_key + "%\' AND d.isLocked = \'0\'";
//            List<Object> results = DsartB4Dao.getInstance().entity().createQuery("SELECT d FROM Dsartb4 d WHERE d.isLocked = \'0\' ORDER BY d.nuart ASC").getResultList();
            List<Object> results = (List<Object>) sp.dao.DsartB4Dao.getInstance().entity().createQuery(sql1).getResultList();
            if (results.isEmpty()) {
                sp.model.TableEntryModel tabModel = new sp.model.TableEntryModel();
                tableUpdate1.getTabelUpdate().setModel(tabModel);
                JOptionPane.showMessageDialog(this, "DSART Kosong");
            } else {
                JOptionPane.showMessageDialog(this, "Ditemukan " + results.size() + " data");
                List<sp.model.Dsartb4> data = new ArrayList<>();
                int count = results.size();
                for (int i = 0; i < count; i++) {
                    data.add((sp.model.Dsartb4) results.get(i));
                }
                sp.model.TableEntryModel tabModel = new sp.model.TableEntryModel();
                tableUpdate1.getTabelUpdate().setModel(tabModel);
                ((sp.model.TableEntryModel) tableUpdate1.getTabelUpdate().getModel()).setData(data);
            }
            tableUpdate1.setLebarKolom2();
            refreshTxt = "Updating table loaded. Found " + results.size() + " data";
        } catch (Exception ep) {
            refreshTxt = "Failed to load updating table " + ep.getMessage();
            System.out.println(refreshTxt);
        }
        try {
            sp.util.LogMessage.write(refreshTxt);
        } catch (IOException ex) {
            Logger.getLogger(sp.controller.UpdateController21.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    /**
     * @return the backButton1
     */

}
